<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Python," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="面试了Python开发，对于多线程的理解还是有不少问题。恶补了一下午，跟着Demo做了一个线程池，还是能达到效果。">
<meta property="og:type" content="article">
<meta property="og:title" content="Python线程池的实现">
<meta property="og:url" content="/2017/02/07/Python线程池的实现/index.html">
<meta property="og:site_name" content="Porson's Bolg">
<meta property="og:description" content="面试了Python开发，对于多线程的理解还是有不少问题。恶补了一下午，跟着Demo做了一个线程池，还是能达到效果。">
<meta property="og:updated_time" content="2017-02-23T03:31:11.750Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Python线程池的实现">
<meta name="twitter:description" content="面试了Python开发，对于多线程的理解还是有不少问题。恶补了一下午，跟着Demo做了一个线程池，还是能达到效果。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"right","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 'porson',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="/2017/02/07/Python线程池的实现/"/>





  <title> Python线程池的实现 | Porson's Bolg </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta custom-logo">
  
    <div class="site-meta-headline">
      <a>
        <img class="custom-logo-image" src="/images/favicon.png"
             alt="Porson's Bolg"/>
      </a>
    </div>
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Porson's Bolg</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
    
      <p class="site-subtitle">MySQL/Python/Golang/Others</p>
    
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user-md"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="/2017/02/07/Python线程池的实现/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Porson">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/images/avatar.gif">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Porson's Bolg">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Porson's Bolg" src="/images/favicon.png">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Python线程池的实现
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-02-07T17:10:33+08:00">
                2017-02-07
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/02/07/Python线程池的实现/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2017/02/07/Python线程池的实现/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>面试了Python开发，对于多线程的理解还是有不少问题。恶补了一下午，跟着Demo做了一个线程池，还是能达到效果。</p>
<a id="more"></a>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#coding:utf8</span></div><div class="line"><span class="keyword">import</span> Queue</div><div class="line"><span class="keyword">import</span> sys</div><div class="line"><span class="keyword">import</span> threading</div><div class="line"><span class="keyword">import</span> traceback</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment"># 定义一些Exception，用于自定义异常处理</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">NoResultsPending</span><span class="params">(Exception)</span>:</span></div><div class="line">    <span class="string">"""All works requests have been processed"""</span></div><div class="line">    <span class="keyword">pass</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">NoWorkersAvailable</span><span class="params">(Exception)</span>:</span></div><div class="line">    <span class="string">"""No worket threads available to process remaining requests."""</span></div><div class="line">    <span class="keyword">pass</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">_handle_thread_exception</span><span class="params">(request, exc_info)</span>:</span></div><div class="line">    <span class="string">"""默认的异常处理函数，只是简单的打印"""</span></div><div class="line">    traceback.print_exception(*exc_info)</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment"># classes</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">WorkerThread</span><span class="params">(threading.Thread)</span>:</span></div><div class="line">    <span class="string">"""后台线程，真正的工作线程，从请求队列(requestQueue)中获取work，</span></div><div class="line">    并将执行后的结果添加到结果队列(resultQueue)"""</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, requestQueue, resultQueue, poll_timeout=<span class="number">5</span>, **kwds)</span>:</span></div><div class="line">        threading.Thread.__init__(self, **kwds)</div><div class="line">        <span class="string">'''设置为守护进行'''</span></div><div class="line">        self.setDaemon(<span class="keyword">True</span>)</div><div class="line">        self._requestQueue = requestQueue</div><div class="line">        self._resultQueue = resultQueue</div><div class="line">        self._poll_timeout = poll_timeout</div><div class="line">        <span class="string">'''设置一个flag信号，用来表示该线程是否还被dismiss,默认为false'''</span></div><div class="line">        self._dismissed = threading.Event()</div><div class="line">        self.start()</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">run</span><span class="params">(self)</span>:</span></div><div class="line">        <span class="string">'''每个线程尽可能多的执行work，所以采用loop，</span></div><div class="line">        只要线程可用，并且requestQueue有work未完成，则一直loop'''</div><div class="line">        <span class="keyword">while</span> <span class="keyword">True</span>:</div><div class="line">            <span class="keyword">if</span> self._dismissed.is_set():</div><div class="line">                <span class="keyword">break</span></div><div class="line">            <span class="keyword">try</span>:</div><div class="line">                <span class="string">'''</span></div><div class="line">                Queue.Queue队列设置了线程同步策略，并且可以设置timeout。</div><div class="line">                一直block，直到requestQueue有值，或者超时</div><div class="line">                '''</div><div class="line">                request = self._requestQueue.get(<span class="keyword">True</span>, self._poll_timeout)</div><div class="line">            <span class="keyword">except</span> Queue.Empty:</div><div class="line">                <span class="keyword">continue</span></div><div class="line">            <span class="keyword">else</span>:</div><div class="line">                <span class="string">'''之所以在这里再次判断dimissed，是因为之前的timeout时间里，很有可能，该线程被dismiss掉了'''</span></div><div class="line">                <span class="keyword">if</span> self._dismissed.is_set():</div><div class="line">                    self._requestQueue.put(request)</div><div class="line">                    <span class="keyword">break</span></div><div class="line">                <span class="keyword">try</span>:</div><div class="line">                    <span class="string">'''执行callable，讲请求和结果以tuple的方式放入requestQueue'''</span></div><div class="line">                    result = request.callable(*request.args, **request.kwds)</div><div class="line">                    <span class="keyword">print</span> self.getName()</div><div class="line">                    self._resultQueue.put((request, result))</div><div class="line">                <span class="keyword">except</span>:</div><div class="line">                    <span class="string">'''异常处理'''</span></div><div class="line">                    request.exception = <span class="keyword">True</span></div><div class="line">                    self._resultQueue.put((request, sys.exc_info()))</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">dismiss</span><span class="params">(self)</span>:</span></div><div class="line">        <span class="string">'''设置一个标志，表示完成当前work之后，退出'''</span></div><div class="line">        self._dismissed.set()</div><div class="line"></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">WorkRequest</span>:</span></div><div class="line">    <span class="string">'''</span></div><div class="line">    @param callable_:，可定制的，执行work的函数</div><div class="line">    @param args: 列表参数</div><div class="line">    @param kwds: 字典参数</div><div class="line">    @param requestID: id</div><div class="line">    @param callback: 可定制的，处理resultQueue队列元素的函数</div><div class="line">    @param exc_callback:可定制的，处理异常的函数</div><div class="line">    '''</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, callable_, args=None, kwds=None, requestID=None,</span></span></div><div class="line">                 callback=None, exc_callback=_handle_thread_exception):</div><div class="line">        <span class="keyword">if</span> requestID == <span class="keyword">None</span>:</div><div class="line">            self.requestID = id(self)</div><div class="line">        <span class="keyword">else</span>:</div><div class="line">            <span class="keyword">try</span>:</div><div class="line">                self.requestID = hash(requestID)</div><div class="line">            <span class="keyword">except</span> TypeError:</div><div class="line">                <span class="keyword">raise</span> TypeError(<span class="string">"requestId must be hashable"</span>)</div><div class="line">        self.exception = <span class="keyword">False</span></div><div class="line">        self.callback = callback</div><div class="line">        self.exc_callback = exc_callback</div><div class="line">        self.callable = callable_</div><div class="line">        self.args = args <span class="keyword">or</span> []</div><div class="line">        self.kwds = kwds <span class="keyword">or</span> &#123;&#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__str__</span><span class="params">(self)</span>:</span></div><div class="line">        <span class="keyword">return</span> <span class="string">"WorkRequest id=%s args=%r kwargs=%r exception=%s"</span> % \</div><div class="line">               (self.requestID, self.args, self.kwds, self.exception)</div><div class="line"></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">ThreadPool</span>:</span></div><div class="line">    <span class="string">'''</span></div><div class="line">    @param num_workers:初始化的线程数量</div><div class="line">    @param q_size,resq_size: requestQueue和result队列的初始大小</div><div class="line">    @param poll_timeout: 设置工作线程WorkerThread的timeout，也就是等待requestQueue的timeout</div><div class="line">    '''</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, num_workers, q_size=<span class="number">0</span>, resq_size=<span class="number">0</span>, poll_timeout=<span class="number">5</span>)</span>:</span></div><div class="line">        self._requestQueue = Queue.Queue(q_size)</div><div class="line">        self._resultQueue = Queue.Queue(resq_size)</div><div class="line">        self.workers = []</div><div class="line">        self.dismissedWorkers = []</div><div class="line">        self.workRequests = &#123;&#125;  <span class="comment"># 设置个字典，方便使用</span></div><div class="line">        self.createWorkers(num_workers, poll_timeout)</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">createWorkers</span><span class="params">(self, num_workers, poll_timeout=<span class="number">5</span>)</span>:</span></div><div class="line">        <span class="string">'''创建num_workers个WorkThread,默认timeout为5'''</span></div><div class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> range(num_workers):</div><div class="line">            self.workers.append(WorkerThread(self._requestQueue, self._resultQueue, poll_timeout=poll_timeout))</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">dismissWorkers</span><span class="params">(self, num_workers, do_join=False)</span>:</span></div><div class="line">        <span class="string">'''停用num_workers数量的线程，并加入dismiss_list'''</span></div><div class="line">        dismiss_list = []</div><div class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> range(min(num_workers, len(self.workers))):</div><div class="line">            worker = self.workers.pop()</div><div class="line">            worker.dismiss()</div><div class="line">            dismiss_list.append(worker)</div><div class="line">        <span class="keyword">if</span> do_join:</div><div class="line">            <span class="keyword">for</span> worker <span class="keyword">in</span> dismiss_list:</div><div class="line">                worker.join()</div><div class="line">        <span class="keyword">else</span>:</div><div class="line">            self.dismissedWorkers.extend(dismiss_list)</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">joinAllDismissedWorkers</span><span class="params">(self)</span>:</span></div><div class="line">        <span class="string">'''join 所有停用的thread'''</span></div><div class="line">        <span class="comment"># print len(self.dismissedWorkers)</span></div><div class="line">        <span class="keyword">for</span> worker <span class="keyword">in</span> self.dismissedWorkers:</div><div class="line">            worker.join()</div><div class="line">        self.dismissedWorkers = []</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">putRequest</span><span class="params">(self, request, block=True, timeout=None)</span>:</span></div><div class="line">        <span class="keyword">assert</span> isinstance(request, WorkRequest)</div><div class="line">        <span class="keyword">assert</span> <span class="keyword">not</span> getattr(request, <span class="string">'exception'</span>, <span class="keyword">None</span>)</div><div class="line">        <span class="string">'''当queue满了，也就是容量达到了前面设定的q_size,它将一直阻塞，直到有空余位置，或是timeout'''</span></div><div class="line">        self._requestQueue.put(request, block, timeout)</div><div class="line">        self.workRequests[request.requestID] = request</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">poll</span><span class="params">(self, block=False)</span>:</span></div><div class="line">        <span class="keyword">while</span> <span class="keyword">True</span>:</div><div class="line">            <span class="keyword">if</span> <span class="keyword">not</span> self.workRequests:</div><div class="line">                <span class="keyword">raise</span> NoResultsPending</div><div class="line">            <span class="keyword">elif</span> block <span class="keyword">and</span> <span class="keyword">not</span> self.workers:</div><div class="line">                <span class="keyword">raise</span> NoWorkersAvailable</div><div class="line">            <span class="keyword">try</span>:</div><div class="line">                <span class="string">'''默认只要resultQueue有值，则取出，否则一直block'''</span></div><div class="line">                request, result = self._resultQueue.get(block=block)</div><div class="line">                <span class="keyword">if</span> request.exception <span class="keyword">and</span> request.exc_callback:</div><div class="line">                    request.exc_callback(request, result)</div><div class="line">                <span class="keyword">if</span> request.callback <span class="keyword">and</span> <span class="keyword">not</span> (request.exception <span class="keyword">and</span> request.exc_callback):</div><div class="line">                    request.callback(request, result)</div><div class="line">                <span class="keyword">del</span> self.workRequests[request.requestID]</div><div class="line">            <span class="keyword">except</span> Queue.Empty:</div><div class="line">                <span class="keyword">break</span></div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">wait</span><span class="params">(self)</span>:</span></div><div class="line">        <span class="keyword">while</span> <span class="keyword">True</span>:</div><div class="line">            <span class="keyword">try</span>:</div><div class="line">                self.poll(<span class="keyword">True</span>)</div><div class="line">            <span class="keyword">except</span> NoResultsPending:</div><div class="line">                <span class="keyword">break</span></div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">workersize</span><span class="params">(self)</span>:</span></div><div class="line">        <span class="keyword">return</span> len(self.workers)</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">stop</span><span class="params">(self)</span>:</span></div><div class="line">        <span class="string">'''join 所有的thread,确保所有的线程都执行完毕'''</span></div><div class="line">        self.dismissWorkers(self.workersize(), <span class="keyword">True</span>)</div><div class="line">        self.joinAllDismissedWorkers()</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment"># 测试代码</span></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    <span class="keyword">import</span> random</div><div class="line">    <span class="keyword">import</span> time</div><div class="line">    <span class="keyword">import</span> datetime</div><div class="line"></div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">do_work</span><span class="params">(data)</span>:</span></div><div class="line">        time.sleep(random.randint(<span class="number">1</span>, <span class="number">3</span>))</div><div class="line">        res = str(datetime.datetime.now()) + <span class="string">""</span> + str(data)</div><div class="line">        <span class="keyword">return</span> res</div><div class="line"></div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">def</span> <span class="title">print_result</span><span class="params">(request, result)</span>:</span></div><div class="line">        <span class="keyword">print</span> <span class="string">"---Result from request %s : %r"</span> % (request.requestID, result)</div><div class="line"></div><div class="line"></div><div class="line">    main = ThreadPool(<span class="number">3</span>)</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">40</span>):</div><div class="line">        req = WorkRequest(do_work, args=[i], kwds=&#123;&#125;, callback=print_result)</div><div class="line">        main.putRequest(req)</div><div class="line">        <span class="keyword">print</span> <span class="string">"work request #%s added."</span> % req.requestID</div><div class="line"></div><div class="line">    <span class="keyword">print</span> <span class="string">'-'</span> * <span class="number">20</span>, main.workersize(), <span class="string">'-'</span> * <span class="number">20</span></div><div class="line"></div><div class="line">    counter = <span class="number">0</span></div><div class="line">    <span class="keyword">while</span> <span class="keyword">True</span>:</div><div class="line">        <span class="keyword">try</span>:</div><div class="line">            time.sleep(<span class="number">0.5</span>)</div><div class="line">            main.poll()</div><div class="line">            <span class="keyword">if</span> (counter == <span class="number">5</span>):</div><div class="line">                <span class="keyword">print</span> <span class="string">"Add 3 more workers threads"</span></div><div class="line">                main.createWorkers(<span class="number">3</span>)</div><div class="line">                <span class="keyword">print</span> <span class="string">'-'</span> * <span class="number">20</span>, main.workersize(), <span class="string">'-'</span> * <span class="number">20</span></div><div class="line">            <span class="keyword">if</span> (counter == <span class="number">10</span>):</div><div class="line">                <span class="keyword">print</span> <span class="string">"dismiss 2 workers threads"</span></div><div class="line">                main.dismissWorkers(<span class="number">2</span>)</div><div class="line">                <span class="keyword">print</span> <span class="string">'-'</span> * <span class="number">20</span>, main.workersize(), <span class="string">'-'</span> * <span class="number">20</span></div><div class="line">            counter += <span class="number">1</span></div><div class="line">        <span class="keyword">except</span> NoResultsPending:</div><div class="line">            <span class="keyword">print</span> <span class="string">"no pending results"</span></div><div class="line">            <span class="keyword">break</span></div><div class="line"></div><div class="line">    main.stop()</div><div class="line">    <span class="keyword">print</span> <span class="string">"Stop"</span></div></pre></td></tr></table></figure>
      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Python/" rel="tag"># Python</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/02/07/Go语言自制简易WebServer/" rel="next" title="Go语言自制简易WebServer">
                <i class="fa fa-chevron-left"></i> Go语言自制简易WebServer
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/02/23/Ubuntu16-04-安装hexo方法/" rel="prev" title="Ubuntu16.04 安装hexo方法">
                Ubuntu16.04 安装hexo方法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2017/02/07/Python线程池的实现/"
           data-title="Python线程池的实现" data-url="/2017/02/07/Python线程池的实现/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="Porson" />
          <p class="site-author-name" itemprop="name">Porson</p>
          <p class="site-description motion-element" itemprop="description">DBA/Python程序员/开源爱好者</p>
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">14</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">10</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/porson" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        
          <div class="cc-license motion-element" itemprop="license">
            <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank">
              <img src="/images/cc-by-nc-sa.svg" alt="Creative Commons" />
            </a>
          </div>
        

        
        

        


      </section>

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  <span class="author" itemprop="copyrightHolder">Create by Porson </span>
    <span class="with-love">
    <i class="fa fa-copyright"></i>
  </span>
   2016 - 
  <span itemprop="copyrightYear">2017</span>
  All copyright

</div>



        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"lpx"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  












  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

  


</body>
</html>
